package coreJob.common;

import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Date;
import java.util.Set;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.matchers.GroupMatcher;

public class Tool {
	
	@SuppressWarnings("static-access")
	public static Trigger getMinutelyTrigger(Date startTime) {
		int randomNum=(int)(Math.random()*100); //100以内的随机数
		if(startTime==null){
			startTime=new Date();
		}
		Trigger trigger = newTrigger().withIdentity("Minutely_"+startTime.getTime()+randomNum,
				"MinutelyTriggerGroup").startAt(startTime).withDescription("每分钟执行一次")
				.withSchedule(
						SimpleScheduleBuilder.simpleSchedule()
								.repeatMinutelyForever(1)).build();
		return trigger;
	}
	@SuppressWarnings("static-access")
	public static Trigger getHourlyTrigger(Date startTime) {
		int randomNum=(int)(Math.random()*100); //100以内的随机数
		if(startTime==null){
			startTime=new Date();
		}
		Trigger trigger = newTrigger().withIdentity("Hourly_"+startTime.getTime()+randomNum,
				"HourlyTriggerGroup").startAt(startTime).withDescription("每小时执行一次")
				.withSchedule(
						SimpleScheduleBuilder.simpleSchedule()
								.repeatHourlyForever(1)).build();
		return trigger;
	}
	@SuppressWarnings("static-access")
	public static Trigger getDaylyTrigger(Date startTime) {
		int randomNum=(int)(Math.random()*100); //100以内的随机数
		if(startTime==null){
			startTime=new Date();
		}
		Trigger trigger = newTrigger().withIdentity("Dayly_"+startTime.getTime()+randomNum,
				"DaylyTriggerGroup").startAt(startTime).withDescription("每天执行一次")
				.withSchedule(
						SimpleScheduleBuilder.simpleSchedule()
								.repeatHourlyForever(24)).build();
		return trigger;
	}
	@SuppressWarnings({ "static-access", "unused" })
	 public static Trigger getCronTrigger(Date startTime, String cronString, String cronDescription) {
    if (startTime == null) {
      startTime = new Date();
    }
    CronTrigger trigger = 
      (CronTrigger)TriggerBuilder.newTrigger()
      .withIdentity("CronTrigger_" + String.valueOf(java.lang.Math.random()).split("\\.")[1], "CronTriggerGroup")
      .startAt(startTime)
      .withDescription(cronDescription == null ? "按Cron计划执行,此Job没有配置Cron说明" : cronDescription)
      .withSchedule(CronScheduleBuilder.cronSchedule(cronString)).build();
    return trigger;
  }
	
    /**
     * 判断此Job是否在执行中
     * @param jobName
     * @param jobGroup
     * @return
     */
	public static boolean ifRunJob(String jobName,String jobGroup){
		try{
			Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
			GroupMatcher<JobKey> matcher = GroupMatcher.anyJobGroup();
			Set<JobKey> jobKeys = scheduler.getJobKeys(matcher);
			
			for (JobKey jobKey : jobKeys) {
				if(jobName.equals(jobKey.getName()) && jobGroup.equals(jobKey.getGroup())){
					//执行中
					return true;
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 毫秒数转化为中文显示
	 * @param mss
	 * @return
	 */
	public static String formatDuring(long mss) {
		String returnValue="";
	    long days = mss / (1000 * 60 * 60 * 24);  
	    long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);  
	    long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);  
	    long seconds = (mss % (1000 * 60)) / 1000;
	    if(days!=0l){
	    	returnValue+=days+"天";
	    }if(hours!=0l){
	    	returnValue+=hours+"小时";
	    }if(minutes!=0l){
	    	returnValue+=minutes+"分";
	    }if(seconds!=0l){
	    	returnValue+=seconds+"秒";
	    }
	    return returnValue;  
	}
	public static boolean isEmpty(Object obj){
		if(obj==null || obj.toString().isEmpty()){
			return true;
		}else{
			return false;
		}
	}
	public static boolean isNotEmpty(Object obj){
		return !isEmpty(obj);
	}
}
